<template>
  <div class="app-container">
    <!--部门分组列表查询条件-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="序号" prop="orderNum">
          <el-input
            v-model="queryParams.orderNum"
            placeholder="请输入序号模糊查询"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="queryParams.sysNormalDisable"  placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="部门分组" prop="comnDictDeptGroup">
          <el-select v-model="queryParams.comnDictDeptGroup"  placeholder="请选择部门分组" clearable>
            <el-option
              v-for="dict in dict.type.comn_dict_dept_group"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="班组名称" prop="fkDeptId">
          <treeselect v-model="queryParams.fkDeptId" :options="groupsOptions"
                      clearable :show-count="true"
                      placeholder="请选择班组名称" style="width: 250px"/>
        </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--批量操作按钮-->
    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['comn:deptGroup:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['comn:deptGroup:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['comn:deptGroup:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['comn:deptGroup:export']"
        >导出
        </el-button>
      </el-col>

      <!--列表右侧上方小控件-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <!--部门分组列表展示-->
    <el-table v-loading="loading" stripe :data="deptGroupList" @selection-change="handleSelectionChange" :highlight-current-row="true">

      <el-table-column type="selection" width="50" align="center" fixed="left"/>

      <el-table-column type="expand" width="80">
        <template slot-scope="props">
          <el-form label-position="left" class="demo-table-expand">
            <el-form-item label="备注：">
              <span>{{ props.row.docNote }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column label="序号" align="center" prop="orderNum" width="80"/>

      <el-table-column label="部门分组" align="center" prop="comnDictDeptGroup">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.comn_dict_dept_group" :value="scope.row.comnDictDeptGroup"/>
        </template>
      </el-table-column>

      <el-table-column label="班组名称" align="center" prop="deptName"/>

      <el-table-column label="状态" align="center" prop="sysNormalDisable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['comn:deptGroup:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['comn:deptGroup:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--列表分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门分组对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal=false>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="序号" prop="orderNum">
          <el-input v-model="form.orderNum" clearable placeholder="请输入序号" :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="部门分组" prop="comnDictDeptGroup">
          <el-select v-model="form.comnDictDeptGroup" clearable filterable placeholder="请选择部门分组"
                     :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.comn_dict_dept_group"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="班组名称" prop="fkDeptId">
          <treeselect v-model="form.fkDeptId" :options="groupsOptions" clearable :show-count="true"
                      placeholder="请选择班组名称" :style="{width: '100%'}"/>
        </el-form-item>

        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" placeholder="请选择状态" clearable filterable :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" placeholder="请输入备注" :style="{width: '100%'}" type="textarea" autosize/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
  import {listDeptGroup, getDeptGroup, delDeptGroup, addDeptGroup, updateDeptGroup} from "@/api/comn/deptGroup";
  import {treeselect} from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    name: "DeptGroup",
    components: {Treeselect},
    dicts: ['comn_dict_dept_group', 'sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 部门分组表格数据
        deptGroupList: [],
        //分组下拉
        groupsOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          comnDictDeptGroup: null,
          orderNum: null,
          fkDeptId: null,
          sysNormalDisable: null,
          docTitle: null,
          docNum: null,
          docDt: null,
          docNote: null,
          confirmDt: null,
          oaDictConfirmResult: null,
          confirmNote: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          comnDictDeptGroup: [
            {required: true, message: "部门分组不能为空", trigger: "change"}
          ],
          fkDeptId: [
            {required: true, message: "关联部门表不能为空", trigger: "change"}
          ],
          sysNormalDisable: [
            {required: true, message: "状态不能为空", trigger: "change"}
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getTreeselect();
    },
    methods: {
      /** 查询部门分组列表 */
      getList() {
        this.loading = true;
        listDeptGroup(this.queryParams).then(response => {
          this.deptGroupList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.groupsOptions = response.data;
        });
      },

      /**取消按钮*/
      cancel() {
        this.open = false;
        this.reset();
      },

      /**表单重置*/
      reset() {
        this.form = {
          deptGroupUuid: null,
          comnDictDeptGroup: null,
          orderNum: null,
          fkDeptId: null,
          sysNormalDisable: '0',
          docTitle: null,
          docNum: null,
          docDt: null,
          docNote: null,
          confirmDt: null,
          oaDictConfirmResult: null,
          confirmNote: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作(清空查询条件进行查询) */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /**多选框选中数据*/
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.deptGroupUuid)
        this.deptNames = selection.map(item => item.deptName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 列表数据新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加部门分组";
      },

      /** 列表数据修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const deptGroupUuid = row.deptGroupUuid || this.ids
        getDeptGroup(deptGroupUuid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改部门分组";
        });
      },

      /** 新增或修改表单提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.deptGroupUuid != null) {
              updateDeptGroup(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addDeptGroup(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },

      /** 列表数据删除按钮操作 */
      handleDelete(row) {
        const deptGroupUuids = row.deptGroupUuid || this.ids;
        const deptNames = row.deptName || this.deptNames;
        this.$modal.confirm('是否确认删除班组名称为"' + deptNames + '"的数据项？', '删除确认').then(function () {
          return delDeptGroup(deptGroupUuids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },

      /** 列表数据导出按钮操作 */
      handleExport() {
        this.download('comn/deptGroup/export', {
          ...this.queryParams
        }, `comn_deptGroup.xlsx`)
      }
    },
  };
</script>
